package com.jyw.business.de.service.impl;

import com.jyw.business.de.domain.DeDemandAllocationConfig;
import com.jyw.business.de.domain.dto.web.DemandAllocationConfigDto;
import com.jyw.business.de.domain.vo.web.DemandAllocationConfig;
import com.jyw.business.de.domain.vo.web.DemandTypeAllocationUser;
import com.jyw.business.de.service.IDeDemandAllocationConfigService;
import com.jyw.business.de.service.ISysDemandAllocationConfigService;
import com.jyw.common.constant.ConfigKeyConstant;
import com.jyw.common.constant.SysDictTypeConstant;
import com.jyw.common.core.domain.entity.SysDictData;
import com.jyw.common.core.domain.model.SimpleDto;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.utils.DictUtils;
import com.jyw.system.domain.SysConfig;
import com.jyw.system.service.ISysConfigService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysDemandAllocationConfigServiceImpl implements ISysDemandAllocationConfigService {

    @Resource
    private ISysConfigService configService;

    @Resource
    private IDeDemandAllocationConfigService allocationConfigService;

    @Override
    public DemandAllocationConfig getConfig() {
        //获取需求自动分配配置
        //设置开关
        Boolean open = Boolean.valueOf(configService.selectConfigByKey(ConfigKeyConstant.DEMAND_ALLOCATION_OPEN));
        DemandAllocationConfig demandAllocationConfig = new DemandAllocationConfig();
        demandAllocationConfig.setOpen(open);
        //获取数据字典
        List<SysDictData> dictCache = DictUtils.getDictCache(SysDictTypeConstant.DE_DEMAND_TYPE);
        //获取当前设置的数据
        List<DeDemandAllocationConfig> allocationConfigList = allocationConfigService.list();
        demandAllocationConfig.setDemandConfig(dictCache.stream().map(data ->
                new DemandTypeAllocationUser(data, allocationConfigList)
        ).collect(Collectors.toList()));
        return demandAllocationConfig;
    }

    @Override
    public void setOpen(SimpleDto<Boolean> dto) {
        //判断是否为空
        if(ObjectUtils.isEmpty(dto.getData())){
           throw new ServiceException("开关不能为空");
        }
        //查询配置
        SysConfig config = configService.findConfigByKey(ConfigKeyConstant.DEMAND_ALLOCATION_OPEN);
        config.setConfigValue(dto.getData().toString());
        configService.updateConfig(config);
    }

    @Override
    public void setAllocation(DemandAllocationConfigDto dto) {
        //查询数据字典是否存在
        DeDemandAllocationConfig data = allocationConfigService.lambdaQuery().eq(DeDemandAllocationConfig::getDictValue, dto.getDictValue()).one();
        if(ObjectUtils.isEmpty(data)){
            DeDemandAllocationConfig config = new DeDemandAllocationConfig();
            config.setDictValue(dto.getDictValue());
            config.setAllocationUser(dto.getUsers().stream().map(user->user.toString()).collect(Collectors.joining(",")));
            allocationConfigService.save(config);
        }else{
            data.setAllocationUser(dto.getUsers().stream().map(user->user.toString()).collect(Collectors.joining(",")));
            allocationConfigService.updateById(data);
        }
    }
}
